package jdos.cpu;

import jdos.Dosbox;
import jdos.hardware.Memory;
import jdos.hardware.RAM;
import jdos.hardware.VGA_memory;
import jdos.ints.EMS;
import jdos.misc.Log;
import jdos.misc.setup.Config;
import jdos.types.LogSeverities;
import jdos.types.LogTypes;
import jdos.util.IntRef;

public class Paging {    

	public static /*Bit8u*/int mem_readb_inline(/*PhysPt*/int address) {
		if (!Memory.memory.a20.enabled) {
			address = address & ~(0x100000);
		}
		if (address >= 0xB8000 && address < 0xB8000 + 0x8000) {
			return VGA_memory.readbB8(address - 0xB8000);
		} else if (address >= 0xA0000 && address < 0xA0000 + 0x10000) {
			return VGA_memory.readbA0(address - 0xA0000);
		} else if (address >= 0xE0000 && address < 0xE0000 + 0x10000) {
			return EMS.readb(address - 0xE0000);
		} else {
			return RAM.readb(address);
		}
	}

    public static /*Bit16u*/int mem_readw_inline(/*PhysPt*/int address) {
        int result = mem_readb_inline(address);
        result |= mem_readb_inline(address+1) << 8;
        return result;
    }

    public static /*Bit32u*/int mem_readd_inline(/*PhysPt*/int address) {
        int result = mem_readb_inline(address);
        result |= (mem_readb_inline(address+1) << 8);
        result |= (mem_readb_inline(address+2) << 16);
        result |= (mem_readb_inline(address+3) << 24);
        return result;
    }

	public static void mem_writeb_inline(/*PhysPt*/int address, /*Bit8u*/int val) {
		if (!Memory.memory.a20.enabled) {
			address = address & ~(0x100000);
		}
		if (address >= 0xB8000 && address < 0xB8000 + 0x8000) {
			VGA_memory.writebB8(address - 0xB8000, val);
		} else if (address >= 0xA0000 && address < 0xA0000 + 0x10000) {
			VGA_memory.writebA0(address - 0xA0000, val);
		} else if (address >= 0xE0000 && address < 0xE0000 + 0x10000) {
			EMS.writeb(address - 0xE0000, val);
		} else {
			RAM.writeb(address, val);
		}
	}

    public static void mem_writew_inline(/*PhysPt*/int address,/*Bit16u*/int val) {
        Paging.mem_writeb_inline(address,(short)(val & 0xFF));val>>=8;
        Paging.mem_writeb_inline(address+1,(short)(val & 0xFF));
    }

    public static void mem_writed_inline(/*PhysPt*/int address,/*Bit32u*/int val) {
        Paging.mem_writeb_inline(address++,val);val>>=8;
        Paging.mem_writeb_inline(address++,val);val>>=8;
        Paging.mem_writeb_inline(address++,val);val>>=8;
        Paging.mem_writeb_inline(address,val);
    }

}
